package com.iring.controller.admin;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iring.controller.BaseController;
import com.iring.domain.common.PayList;
import com.iring.domain.common.view.OrderView;
import com.iring.domain.pay.res.PaypalRes;
import com.iring.vo.common.BooleanResultVO;
import com.iring.vo.pay.PayBackVO;
import com.iring.vo.pay.PayVO;


/**
 * 通用支付Controller
 *
 * @author wanggan
 */
@RestController
@RequestMapping(value = "/admin/pay",produces = "application/json")
public class PayController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(PayController.class);

    /**
     * 获取充值信息列表
     *
     * @return 显示所有的充值列表信息
     */
    @RequestMapping(value = "/list-all-pay-info", method = RequestMethod.POST)
    public List<PayList> listAllPayInfo() {
        return payListDao.listAllPayInfo();
    }

    /**
     * 查询其他金额的内容
     *
     * @param payVO
     *            需要进行查询的金额
     * @return 对应金额打折后的金额信息
     */
    @RequestMapping(value = "/show-other-pay-info", method = RequestMethod.POST)
    public PayList showOtherPayInfo(@RequestBody PayVO payVO) {
        return payListDao.showOtherPayInfo(payVO.getOptamounts());
    }

    /**
     * 显示所有的订单列表信息
     *
     * @return  订单列表
     */
    @RequestMapping(value = "/list-all-order", method = RequestMethod.POST)
    public List<OrderView> listAllOrder() {
        return orderViewService.listAllOrder(getUserSession().getUsername());
//        return orderViewService.listAllOrder("test222@qq.com");
    }

    /**
     * 添加paypal支付订单
     *
     * @param payVO
     *            创建paypal订单的请求参数
     * @return 返回订单创建成功后的信息
     */
    @RequestMapping(value = "/add-paypal-order", method = RequestMethod.POST)
    public PayBackVO addPaypalOrder(@RequestBody PayVO payVO) {
//        return payReqService.addPaypalOrder(payVO, getUserSession().getUsername());
//        PaypalRes paypalRes =  payReqService.addPaypalOrder(payVO, "test222@qq.com");
        String username = getUserSession().getUsername();
        PaypalRes paypalRes =  payReqService.addPaypalOrder(payVO, username);
        logService.saveLog(username,"add-paypal-order",getUserRealIp(),true);
        return new PayBackVO(paypalRes.getOrderid(),paypalRes.getOrderHref());
    }

    @RequestMapping(value = "/repay-paypal-order", method = RequestMethod.GET)
    public PayBackVO repayPaypalOrder(@RequestParam("orderid")String orderid) {
//        return payOrderDao.findByUsername(getUserSession().getUsername());
        String username = getUserSession().getUsername();
        PayBackVO payBackVO = payReqService.repayPaypalOrder(orderid,username);
        logService.saveLog(username,"repay-paypal-order",getUserRealIp(),true);
        return payBackVO;
//        return payReqService.repayPaypalOrder(orderid,"test222@qq.com");
    }



    @RequestMapping(value = "/add-alipay", method = RequestMethod.POST)
    public PayBackVO addAlipay(@RequestBody PayVO payVO){
        String username = getUserSession().getUsername();
        PayBackVO payBackVO = payReqService.addAlipay(payVO,username);
        logService.saveLog(username,"add-alipay",getUserRealIp(),true);
        return payBackVO;
//        return payReqService.addAlipay(payVO,"test222@qq.com");
    }




    /**
     * 取消订单
     *
     * @param orderid
     *          订单编号
     * @return  取消结果，取消成功<code>true</code>
     * 取消失败<code>fasle</code>
     */
    @RequestMapping(value = "/cancel-order", method = RequestMethod.GET)
    public BooleanResultVO cancelOrder(@RequestParam("orderid")String orderid) {
        String username = getUserSession().getUsername();
        Boolean isSuccess = payReqService.cancelOrder(orderid,username);
        logService.saveLog(username,"cancel-order",getUserRealIp(),isSuccess,"orderid="+orderid);
//        Boolean isSuccess = payReqService.cancelOrder(orderid,"test222@qq.com");
        return new BooleanResultVO(isSuccess);
    }
}
